package com.erpanderp.wam.usm.view.user;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.erpanderp.wam.ac.model.service.travel.TravelRequestDAO;
import com.erpanderp.wam.cm.model.entity.SAPCertification;
import com.erpanderp.wam.usm.model.entity.user.Education;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.entity.user.EmployeeRole;
import com.erpanderp.wam.usm.model.entity.user.Employment;
import com.erpanderp.wam.usm.model.entity.user.NewJoinee;
import com.erpanderp.wam.usm.model.entity.user.PassportAndPANDetails;
import com.erpanderp.wam.usm.model.entity.user.Reference;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EducationDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
import com.erpanderp.wam.usm.model.service.user.EmploymentDAO;
import com.erpanderp.wam.usm.model.service.user.NewJoineeDAO;
import com.erpanderp.wam.usm.model.service.user.PassportAndPANDAO;
import com.erpanderp.wam.usm.model.service.user.ReferenceDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.WAMHostManager;


@ViewScoped
@ManagedBean
public class EformViewBean {
	
	HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	ServletContext context = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
	
	private Education education = new Education();

	private PassportAndPANDetails passportAndPANDetails = new PassportAndPANDetails();
	private List<Education> educationsList = new ArrayList<Education>();
	private List<Reference> referencesList = new ArrayList<Reference>();
	private List<Employment> employmentsList = new ArrayList<Employment>();
	private NewJoinee newJoinee;
	private Date date =new Date();
	
	
	EducationDAO educationDAO = new EducationDAO();
	ReferenceDAO referenceDAO = new ReferenceDAO();
	EmploymentDAO employmentDAO = new EmploymentDAO();
	PassportAndPANDAO passportAndPANDAO = new PassportAndPANDAO(); 
	private EmailUtil emailUtil = new EmailUtil();
	private  String EMAIL_MESSAGE;
    private Employee centerHead;
    Long newJoineeId;
	
	public EformViewBean(){
	
        centerHead =new EmployeeAndReportingDAO().getCenterHeadByRole();
        System.out.println("CENTER HEAD NAME "+centerHead.getFirstName());
        newJoinee = (NewJoinee) session.getAttribute("newJoinee");
		
		newJoinee = new NewJoineeDAO().getNewJoineeById(newJoinee.getNewJoineeId());
		System.out.println("NEW JOINEE NAME ="+newJoinee.getFirstName());
        educationsList= educationDAO.getEducations(newJoinee.getNewJoineeId());
		
	    referencesList = referenceDAO.getReferences(newJoinee.getNewJoineeId());
	    System.out.println("REFERENCE LIST SIZE IS"+referencesList.size());
	    
	    employmentsList = employmentDAO.getEmployments(newJoinee.getNewJoineeId());   
	    passportAndPANDetails = passportAndPANDAO.getPassportAndPANDetails(newJoinee.getNewJoineeId());
	    System.out.println("passport;;;;;;;;;;;;;;;;;;;;;;;;;;;;:" +passportAndPANDetails );
				
		  }
	
	
	
	
	private String name;
	
	public void EformviewBean() {
		
		// TODO Auto-generated constructor stub
		System.out.println("UPLOAD MASTER DATA BEAN INTIALIZING");
	}

	// File sss= new File(extContext.getRealPath("//lettersofauthorization//"));
	// File rootPath= new File(extContext.getRealPath("//lettersofauthorization//"));

	public void markAsValidNewJoinee(){
	     
		//String[] to={employee.getEmail()};
		Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);

		emailUtil = new EmailUtil();
		if(hostsMap.get(CommonLookup.DEV_EMAIL) == null){

		String[] to = {centerHead.getEmail()};
		emailUtil.setRecieverAddresses(to);
	}else{
		String[] to = {hostsMap.get(CommonLookup.DEV_EMAIL)};
		emailUtil.setRecieverAddresses(to);
	}
 		
 		String messageString = "Dear " + centerHead.getFirstName() + " " + centerHead.getLastName() + "" +
 								",<br /><br />" +
 								
 								"<b>New Joinee Details</b><br/><br/>"+
 								"<table style='border: 1px solid #d9d9d9;'>" +
 						        "<tr> <td>Name  </td><td> : </td><td>" + newJoinee.getFirstName()+ " "+newJoinee.getLastName() +"</td></tr>" +
 						       
 						       "<tr> <td>Competency</td><td> : </td> <td>" + newJoinee.getPrimarySkill() + " </td></tr>" +
 						        "</table><br/>"+	
 						       
 						   					
 								"Thanks & Regards, <br />"+CommonLookup.WAM_DEVTEAM_SIGNATURE;
 		
 		emailUtil.setMessage(messageString);
 		//emailUtil.setRecieverAddresses(to);
 		emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
 		emailUtil.setSubject("WAM - New Joinee Details");
        
 		SwingWorker worker = new SwingWorker<String, Object>() {
 			@Override
 			protected String doInBackground() throws Exception {
 				EMAIL_MESSAGE = emailUtil.processAndSendEmail();
 				return null;
 			}

 			@Override
 			protected void done() {
 					System.out.println("DONE " + EMAIL_MESSAGE);		
 			}
 		};
 		worker.execute();
 		System.out.println( "BEFORE RETURNING: " + EMAIL_MESSAGE);
 		
 		
		
      
		
	}
	
	

	public void uploadHandler(FileUploadEvent e) throws IOException{
		ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
		String fileName = e.getFile().getFileName();
		
		UploadedFile file = e.getFile();
	
	} 
	
	
	public String viewLetterOfAuthorization1(){
		System.out.println("gggggggggggg");
    	return "letterofauthorization1";

		
	}

	public String viewLetterOfAuthorization2(){
		System.out.println("fffffffffffffff");
    	return "letterofauthorization2";

		
	}
	


	public List<Education> getEducationsList() {
		return educationsList;
	}



	public void setEducationsList(List<Education> educationsList) {
		this.educationsList = educationsList;
	}



	public List<Reference> getReferencesList() {
		return referencesList;
	}



	public void setReferencesList(List<Reference> referencesList) {
		this.referencesList = referencesList;
	}



	public List<Employment> getEmploymentsList() {
		return employmentsList;
	}



	public void setEmploymentsList(List<Employment> employmentsList) {
		this.employmentsList = employmentsList;
	}



	public Education getEducation() {
		return education;
	}



	public void setEducation(Education education) {
		this.education = education;
	}



	public NewJoinee getNewJoinee() {
		return newJoinee;
	}



	public void setNewJoinee(NewJoinee newJoinee) {
		this.newJoinee = newJoinee;
	}



	public PassportAndPANDetails getPassportAndPANDetails() {
		return passportAndPANDetails;
	}



	public void setPassportAndPANDetails(PassportAndPANDetails passportAndPANDetails) {
		this.passportAndPANDetails = passportAndPANDetails;
	}









	public Date getDate() {
		return date;
	}









	public void setDate(Date date) {
		this.date = date;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEMAIL_MESSAGE() {
		return EMAIL_MESSAGE;
	}

	public void setEMAIL_MESSAGE(String eMAIL_MESSAGE) {
		EMAIL_MESSAGE = eMAIL_MESSAGE;
	}

	public EmailUtil getEmailUtil() {
		return emailUtil;
	}

	public void setEmailUtil(EmailUtil emailUtil) {
		this.emailUtil = emailUtil;
	}

	public Employee getCenterHead() {
		return centerHead;
	}

	public void setCenterHead(Employee centerHead) {
		this.centerHead = centerHead;
	}

	public Long getNewJoineeId() {
		return newJoineeId;
	}

	public void setNewJoineeId(Long newJoineeId) {
		this.newJoineeId = newJoineeId;
	}

	

}
